package net.kts.subskill.tel;

import java.util.ArrayList;
import java.util.List;

import net.kts.main.R;
import net.kts.subskill.tel.embassy.Embassy;
import net.kts.transrate.TranslateService;

import org.simpleframework.xml.Serializer;
import org.simpleframework.xml.core.Persister;

import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.content.res.Resources.NotFoundException;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class Call extends ListActivity {
    /** Called when the activity is first created. */
	private CallAdapter callAdapter;
	private ProgressDialog mProgress;
	private List<CallVO> callList;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tel_call);
        mProgress		= new ProgressDialog(this);
        callAdapter 	= new CallAdapter(this);
        callList 		= new ArrayList<CallVO>();
        
        TextView tvLabel = (TextView) findViewById(R.id.tv_tel_Label);
        tvLabel.setText(TranslateService.translateRepository("Emergency call"));
		
		// 대사관 버튼 이벤트 리스너
		Button btnEmbassy = (Button)findViewById(R.id.getEmbList);
        btnEmbassy.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(Call.this, Embassy.class);
				
				startActivity(intent);
			}
		});
        
    	// 번역 실패 시, 디폴트 텍스트 출력
        String strEmbassy = TranslateService.translateRepository("Embassy");
        if(strEmbassy == null){
        }else if(strEmbassy.trim().equals("")){
        }else{
        	btnEmbassy.setText(strEmbassy);
        }
        
        loadList();
		
    }
    
    public void loadList(){
		String wait = TranslateService.translateRepository("잠시만 기다려주세요.");
		if(wait == null){
			mProgress.setMessage("Please wait a moment.");
		}else if(wait.trim().equals("")){
			mProgress.setMessage("Please wait a moment.");
		}else{
			mProgress.setMessage(wait);
		}
    	mProgress.show();
    	
    	
    	final Handler handler = new Handler();
    	new Thread() {
    		@Override
    		public void run() {
    			
    			try {
    				Serializer callSerializer = new Persister();
    				try {
    					// 긴급 전화 xml 리스트 추출
    					CallXml readList = callSerializer.read(CallXml.class, getResources().openRawResource(R.raw.call_emergency));
    					
    					callList = readList.getList();
    					
    					for (CallVO vo : callList) {
							vo.setName(vo.getName());
							vo.setMemo(vo.getMemo());
						}
    					
    					
    					handler.post(mHandler);
    				} catch (NotFoundException e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				} catch (Exception e) {
    					// TODO Auto-generated catch block
    					e.printStackTrace();
    				}
    			} catch (Exception e) {
    				Log.i("log333", "loadList Exception");
    				e.printStackTrace();
    			}
    			
//    			mHandler.sendMessage(mHandler.obtainMessage(what));
    			
    		}
    	}.start();

    }
    
    private Runnable mHandler = new Runnable() {
		@Override
		public void run() {
			mProgress.dismiss();
    		

			if (callList.size() == 0) {
				Toast.makeText(Call.this, "No call list available", Toast.LENGTH_SHORT).show();
				return;
			}
			// 출력
			
			callAdapter.setData((ArrayList<CallVO>) callList);    			
			setListAdapter(callAdapter);
		}
	};
    	

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		super.onListItemClick(l, v, position, id);
		CallVO item = (CallVO) l.getItemAtPosition(position);
		
		Log.e("kts", "Tel : "+item.getTel());
		startActivity(new Intent(Intent.ACTION_CALL, Uri.parse("tel:"
											+ item.getTel())));
	}
    
}